package cn.com.surker.sale.service.impl;

import cn.com.surker.base.strength.dao.BaseStrengthLevelDao;
import cn.com.surker.base.strength.entity.BaseStrengthLevel;
import cn.com.surker.common.Code;
import cn.com.surker.enums.sale.AdjustPriceTypeEnum;
import cn.com.surker.enums.sale.BasicPriceTypeEnum;
import cn.com.surker.enums.system.StrengthTypeEnum;
import cn.com.surker.sale.dao.SaleContractDao;
import cn.com.surker.sale.entity.SaleContract;
import cn.com.surker.sale.entity.SaleStrengthAdjustPrice;
import cn.com.surker.sale.dao.SaleStrengthAdjustPriceDao;
import cn.com.surker.sale.service.SaleStrengthAdjustPriceBaseService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import javax.annotation.Resource;
import javax.transaction.RollbackException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
* <p>
    * 销售合同信息标号调整价 服务实现类
    * </p>
*
* @author admin
* @since 2024-08-01
*/
@Service
public abstract class SaleStrengthAdjustPriceBaseServiceImpl extends ServiceImpl<SaleStrengthAdjustPriceDao, SaleStrengthAdjustPrice> implements SaleStrengthAdjustPriceBaseService {

    @Resource
    protected SaleStrengthAdjustPriceDao strengthAdjustPriceDao;
    @Resource
    private BaseStrengthLevelDao strengthLevelDao;
    @Resource
    private SaleContractDao saleContractDao;


    /**
     * 根据合同信息查询调整价格
     * @param contractId
     * @return
     */
    public List<SaleStrengthAdjustPrice> findContract(Long contractId, Integer type) {
        if(StringUtils.isNull(contractId))
            return null;
//        if (StringUtils.isNull(type))
//            type = AdjustPriceTypeEnum.DYBH.getCode();
        QueryWrapper<SaleStrengthAdjustPrice> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        if (!StringUtils.isNull(type))
            query.eq("type", type);
        query.orderByDesc("take_time");
        List<SaleStrengthAdjustPrice> list = strengthAdjustPriceDao.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 保存调整信息
     * @param strengthAdjustPrice
     * @return
     */
    @Transactional(rollbackFor = RollbackException.class,propagation = Propagation.REQUIRED)
    public Integer saveAdjust(SaleStrengthAdjustPrice strengthAdjustPrice) {
        if (strengthAdjustPrice == null)
            return Code.PARAM_EMPTY.getCode();
        if (StringUtils.isNull(strengthAdjustPrice.getType()))
            return Code.PARAM_EMPTY.getCode();
        SaleContract contract = saleContractDao.selectById(strengthAdjustPrice.getContractId());
        if (contract == null)
            return Code.ERROR_DATA.getCode();
        Date nowDate = new Date();
        if (strengthAdjustPrice.getType().intValue() != contract.getAdjustPriceType().intValue()) {
            contract.setAdjustPriceType(strengthAdjustPrice.getType());
            contract.setUpdator(strengthAdjustPrice.getCreator());
            contract.setUpdateTime(nowDate);
            Integer code = saleContractDao.updateById(contract);
            if (code < 1)
                return -1;
        }

        if (strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.DYBH.getCode().intValue()
                || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDJC.getCode().intValue()
                || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDJCJE.getCode().intValue()
                || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDJCBH.getCode().intValue()
                || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDGDJC.getCode().intValue()
                || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDGDHTJC.getCode().intValue()
                || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDGDHTXF.getCode().intValue()) {
            UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
            uw.set("status", 1);
            uw.set("remark", " " + strengthAdjustPrice.getCreator() + "覆盖新增");
            uw.set("update_time", nowDate);
            uw.set("updator", strengthAdjustPrice.getCreator());
            uw.eq("org_id", strengthAdjustPrice.getOrgId());
            uw.eq("contract_id", strengthAdjustPrice.getContractId());
//            uw.eq("type", strengthAdjustPrice.getType());
//            if (!(strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.DYBH.getCode().intValue()
//                    || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDJC.getCode().intValue()
//                    || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDJCJE.getCode().intValue()
//                    || strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDJCBH.getCode().intValue()))
//                uw.eq("strength", strengthAdjustPrice.getStrength());
            strengthAdjustPriceDao.update(new SaleStrengthAdjustPrice(), uw);

            Integer code = strengthAdjustPriceDao.insert(strengthAdjustPrice);
            if (code > 0)
                return 0;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        } else {
            QueryWrapper<BaseStrengthLevel> query = new QueryWrapper<>();
            query.eq("status", 0);
            query.eq("type", StrengthTypeEnum.HNT.getCode());
            query.orderByAsc("level");
            List<BaseStrengthLevel> list = strengthLevelDao.selectList(query);
            if (list == null || list.size() == 0)
                return -1;
            List<SaleStrengthAdjustPrice> priceList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                BaseStrengthLevel level = list.get(i);
                UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
                uw.set("status", 1);
                uw.set("remark", strengthAdjustPrice.getCreator() + "覆盖新增");
                uw.set("update_time", nowDate);
                uw.set("updator", strengthAdjustPrice.getCreator());
                uw.eq("org_id", strengthAdjustPrice.getOrgId());
                uw.eq("contract_id", strengthAdjustPrice.getContractId());
                uw.eq("type", strengthAdjustPrice.getType());
                uw.eq("strength", level.getLevel());
                strengthAdjustPriceDao.update(new SaleStrengthAdjustPrice(), uw);

                SaleStrengthAdjustPrice adjustPrice_ = new SaleStrengthAdjustPrice(strengthAdjustPrice.getOrgId(), strengthAdjustPrice.getContractId(), level.getStrengthId(),
                        level.getLevel(), strengthAdjustPrice.getTakeTime(), strengthAdjustPrice.getPrice(), strengthAdjustPrice.getTaxPrice(), strengthAdjustPrice.getType(), 0, "",
                        strengthAdjustPrice.getBasisPriceType(), strengthAdjustPrice.getPriceDay(), strengthAdjustPrice.getRate(),strengthAdjustPrice.getCreator(), nowDate);
                priceList.add(adjustPrice_);
            }
            Integer code = strengthAdjustPriceDao.saveBatch(priceList);
            if (code > 0)
                return 0;

            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }
    /**
     * 修改合同调整类型信息
     * @param contractId
     * @param type
     * @param price 变更价格
     * @param rate 变革率
     * @param creator
     * @return
     */
    public Integer updateAdjustType(Long contractId, Integer type, Double price, Double rate, String creator) {
        if (StringUtils.isNull(contractId))
            return -1;
        UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
        if (type.intValue() == AdjustPriceTypeEnum.GDJC.getCode().intValue()
                || type.intValue() == AdjustPriceTypeEnum.JCBH.getCode().intValue()
                || type.intValue() == AdjustPriceTypeEnum.DYBH.getCode()
                || type.intValue() == AdjustPriceTypeEnum.ZDGDJC.getCode()) {
            uw.set("price", price);
            uw.set("tax_price", price);
        } else {
            uw.set("rate", rate);
        }
        uw.set("remark", " " + creator + "覆盖修改");
        uw.set("update_time", new Date());
        uw.set("updator", creator);
        uw.eq("contract_id", contractId);
        uw.eq("type", type);
        Integer code = strengthAdjustPriceDao.update(new SaleStrengthAdjustPrice(), uw);
        if (code > 0)
            return 0;
        return -1;
    }


    /**
     * 修改
     * @return
     */
    public Integer updateAdjustById(SaleStrengthAdjustPrice adjustPrice, String creator) {
        if (adjustPrice == null || StringUtils.isNull(adjustPrice.getId()))
            return -1;
        Date nowDate = new Date();
        UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
        if (adjustPrice.getType().intValue() == AdjustPriceTypeEnum.GDJC.getCode().intValue()
                || adjustPrice.getType().intValue() == AdjustPriceTypeEnum.JCBH.getCode().intValue()
                || adjustPrice.getType().intValue() == AdjustPriceTypeEnum.DYBH.getCode()
                || adjustPrice.getType().intValue() == AdjustPriceTypeEnum.ZDGDJC.getCode()) {
            uw.set("price", adjustPrice.getPrice());
            uw.set("tax_price", adjustPrice.getTaxPrice());
        } else {
            uw.set("rate", adjustPrice.getRate());
        }
        uw.set("remark", " " + creator + "覆盖修改");
        uw.set("update_time", nowDate);
        uw.set("updator", creator);
        uw.eq("id", adjustPrice.getId());
        return 0;
    }

    /**
     * 修改调整信息
     * @param strengthAdjustPrice
     * @return
     */
    @Transactional(rollbackFor = RollbackException.class,propagation = Propagation.REQUIRED)
    public Integer updateAdjust(SaleStrengthAdjustPrice strengthAdjustPrice) {
        if (strengthAdjustPrice == null)
            return Code.PARAM_EMPTY.getCode();

        if (strengthAdjustPrice.getType().intValue() == AdjustPriceTypeEnum.DYBH.getCode().intValue()) {
            SaleStrengthAdjustPrice adjustPrice = strengthAdjustPriceDao.selectById(strengthAdjustPrice.getId());
            if (adjustPrice == null || adjustPrice.getStatus().intValue() == 1)
                return Code.ERROR_DATA.getCode();
            if (!StringUtils.isNull(strengthAdjustPrice.getType()) && adjustPrice.getType().intValue() != strengthAdjustPrice.getType().intValue()) {
                Date nowDate = new Date();
                UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
                uw.set("status", 1);
                uw.set("remark", " " + strengthAdjustPrice.getCreator() + "覆盖新增");
                uw.set("update_time", nowDate);
                uw.set("updator", strengthAdjustPrice.getCreator());
                uw.eq("id", strengthAdjustPrice.getId());
                strengthAdjustPriceDao.update(new SaleStrengthAdjustPrice(), uw);
                strengthAdjustPrice.setId(null);
                Integer code = strengthAdjustPriceDao.insert(strengthAdjustPrice);
                if (code > 0)
                    return 0;

                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return -1;
            }
            if (!StringUtils.isNull(strengthAdjustPrice.getRate()))
                adjustPrice.setRate(strengthAdjustPrice.getRate());
            if (!StringUtils.isNull(strengthAdjustPrice.getPriceDay()))
                adjustPrice.setPriceDay(strengthAdjustPrice.getPriceDay());
            if (!StringUtils.isNull(strengthAdjustPrice.getBasisPriceType()))
                adjustPrice.setBasisPriceType(strengthAdjustPrice.getBasisPriceType());
            if (!StringUtils.isNull(strengthAdjustPrice.getType()))
                adjustPrice.setType(strengthAdjustPrice.getType());
            if (!StringUtils.isNull(strengthAdjustPrice.getPrice()))
                adjustPrice.setPrice(strengthAdjustPrice.getPrice());
            if (!StringUtils.isNull(strengthAdjustPrice.getTaxPrice()))
                adjustPrice.setTaxPrice(strengthAdjustPrice.getTaxPrice());
            if (!StringUtils.isNull(strengthAdjustPrice.getRemark()))
                adjustPrice.setRemark(strengthAdjustPrice.getRemark());
            if (!StringUtils.isNull(strengthAdjustPrice.getTakeTime()))
                adjustPrice.setTakeTime(strengthAdjustPrice.getTakeTime());
            adjustPrice.setUpdator(strengthAdjustPrice.getUpdator());
            adjustPrice.setUpdateTime(new Date());
            Integer code = strengthAdjustPriceDao.updateById(adjustPrice);
            if (code > 0)
                return 0;
            return -1;
        } else {
            QueryWrapper<BaseStrengthLevel> query = new QueryWrapper<>();
            query.eq("status", 0);
            query.eq("type", StrengthTypeEnum.HNT.getCode());
            query.orderByAsc("level");
            List<BaseStrengthLevel> list = strengthLevelDao.selectList(query);
            if (list == null || list.size() == 0)
                return -1;
            Date nowDate = new Date();
            List<SaleStrengthAdjustPrice> priceList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                BaseStrengthLevel level = list.get(i);
                UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
                uw.set("status", 1);
                uw.set("remark", strengthAdjustPrice.getCreator() + "覆盖新增");
                uw.set("update_time", nowDate);
                uw.set("updator", strengthAdjustPrice.getCreator());
                uw.eq("org_id", strengthAdjustPrice.getOrgId());
                uw.eq("contract_id", strengthAdjustPrice.getContractId());
                uw.eq("type", strengthAdjustPrice.getType());
                uw.eq("strength", level.getLevel());
                strengthAdjustPriceDao.update(new SaleStrengthAdjustPrice(), uw);

                SaleStrengthAdjustPrice adjustPrice_ = new SaleStrengthAdjustPrice(strengthAdjustPrice.getOrgId(), strengthAdjustPrice.getContractId(), level.getStrengthId(),
                        level.getLevel(), strengthAdjustPrice.getTakeTime(), strengthAdjustPrice.getPrice(), strengthAdjustPrice.getTaxPrice(), strengthAdjustPrice.getType(), 0, "",
                        strengthAdjustPrice.getBasisPriceType(), strengthAdjustPrice.getPriceDay(), strengthAdjustPrice.getRate(),strengthAdjustPrice.getCreator(), nowDate);
                priceList.add(adjustPrice_);
            }
            Integer code = strengthAdjustPriceDao.saveBatch(priceList);
            if (code > 0)
                return 0;

            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }
    /**
     * 修改调整信息
     * @param strengthAdjustPrice
     * @return
     */
    @Transactional(rollbackFor = RollbackException.class,propagation = Propagation.REQUIRED)
    public Integer updateAdjustV1(SaleStrengthAdjustPrice strengthAdjustPrice) {
        if (strengthAdjustPrice == null || StringUtils.isNull(strengthAdjustPrice.getId()))
            return Code.PARAM_EMPTY.getCode();
        SaleStrengthAdjustPrice adjustPrice = strengthAdjustPriceDao.selectById(strengthAdjustPrice.getId());
        if (adjustPrice == null || adjustPrice.getStatus().intValue() == 1)
            return Code.ERROR_DATA.getCode();
        Integer code = strengthAdjustPriceDao.updateById(strengthAdjustPrice);
        if (code > 0)
            return 0;
        return -1;
    }
    /**
     * 批量保存
     * @return
     */
    public Integer saveBatch(Long orgId, Long contractId, Integer type, Date takeTime, Double price, Double taxPrice,
                             Integer basisPriceType, String priceDay, Double rate, String creator, Integer adjustPriceType) {
        if (StringUtils.isAllParamNull(orgId, contractId, price, rate, taxPrice, basisPriceType, adjustPriceType))
            return Code.PARAM_EMPTY.getCode();
        if (StringUtils.isNull(type))
            type = StrengthTypeEnum.HNT.getCode();
        QueryWrapper<BaseStrengthLevel> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("type", type);
        query.orderByAsc("level");
        List<BaseStrengthLevel> list = strengthLevelDao.selectList(query);
        if (list == null || list.size() == 0)
            return -1;
        Date nowDate = new Date();
        List<SaleStrengthAdjustPrice> priceList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BaseStrengthLevel level = list.get(i);
            UpdateWrapper<SaleStrengthAdjustPrice> uw = new UpdateWrapper<>();
            uw.set("status", 1);
            uw.set("remark", creator + "覆盖新增");
            uw.set("update_time", nowDate);
            uw.set("updator", creator);
            uw.eq("org_id", orgId);
            uw.eq("contract_id", contractId);
            uw.eq("type", adjustPriceType);
            uw.eq("strength", level.getLevel());
            strengthAdjustPriceDao.update(new SaleStrengthAdjustPrice(), uw);

            SaleStrengthAdjustPrice adjustPrice_ = new SaleStrengthAdjustPrice(orgId, contractId, level.getStrengthId(),
                    level.getLevel(), takeTime, price, taxPrice, adjustPriceType, 0, "",
                    basisPriceType, priceDay, rate,creator, nowDate);
            priceList.add(adjustPrice_);
        }
        Integer code = strengthAdjustPriceDao.saveBatch(priceList);
        if (code > 0)
            return 0;
        return -1;
    }
}